package com.epoint.mobileoa.ui.handwrittensign;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.IOHelp;

/**
 * @author liyc
 * @time 2013-1-29 上午9:19:39
 * @annotation 手写笔迹
 */
public class HandwrittenSignDetail extends SuperView {

	Button btSaveWriting,btCleanWriting;
	PaintView myView;
	TextView tvWritingTip;
	static int[] bjcolor = new int[]{Color.BLACK,Color.BLUE,Color.RED,Color.YELLOW,Color.GREEN};
    LinearLayout llColor;
	@Override
	public void initControls() {
		RelativeLayout llWriting = (RelativeLayout) findViewById(R.id.llWriting);
		myView=new PaintView(this); 
		myView.setLayoutParams(new LayoutParams(480, 320));
		llWriting.addView(myView);

		llColor = (LinearLayout) findViewById(R.id.llColor);
		for(int i=0;i<bjcolor.length;i++)
		{
			final int mi = i;
			Button btColor = new Button(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  // , 1是可选写的
			lp.setMargins(10,0,0,0); 
			btColor.setLayoutParams(lp); 
			btColor.setTextColor(getResources().getColor(R.color.灰色));
			btColor.setWidth(50);
			btColor.setHeight(50);
			btColor.setBackgroundColor(bjcolor[mi]);
			btColor.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					myView.paint.setColor(bjcolor[mi]);
					for(int j=1;j<llColor.getChildCount();j++)
					{
						Button b = (Button) llColor.getChildAt(j);
						b.setText("");
					}
					((Button)v).setText("★");
				}
			});
			llColor.addView(btColor);
		}

		final LinearLayout llCx = (LinearLayout) findViewById(R.id.llCx);
		
		final List<CxModel> cxList = new ArrayList<CxModel>();
		CxModel c = new CxModel();
		c.name = "细";
		c.value = 3;
		cxList.add(c);
		c = new CxModel();
		c.name = "中";
		c.value = 7;
		cxList.add(c);
		c = new CxModel();
		c.name = "粗";
		c.value = 10;
		cxList.add(c);
		
		for(int i=0;i<cxList.size();i++)
		{
			final int mi = i;
			Button bt = new Button(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  // , 1是可选写的
			lp.setMargins(10,0,0,0); 
			bt.setLayoutParams(lp); 
			
			bt.setWidth(50);
			bt.setHeight(50);
			bt.setBackgroundColor(getResources().getColor(R.color.灰色));
			bt.setText(cxList.get(mi).name);
			bt.setTextSize(10);
			bt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					myView.paint.setStrokeWidth(cxList.get(mi).value);
					for(int j=1;j<llCx.getChildCount();j++)
					{
						Button bx = (Button) llCx.getChildAt(j);
						bx.setTextColor(getResources().getColor(R.color.black));
					}
					((Button)v).setTextColor(getResources().getColor(R.color.white));
				}
			});
			llCx.addView(bt);
		}

		btSaveWriting = (Button) findViewById(R.id.btSaveWriting);
		btSaveWriting.setOnClickListener(this);

		btCleanWriting = (Button) findViewById(R.id.btCleanWriting);
		btCleanWriting.setOnClickListener(this);

		tvWritingTip = (TextView) findViewById(R.id.tvWritingTip);
	}     

	@Override
	public void onClick(View v) {
		if(v==btSaveWriting)
		{
			Bitmap bitmap = myView.getCachebBitmap();
			byte[] b = IOHelp.Bitmap2Bytes(bitmap);
			IOHelp.saveFile(b, "/sdcard/mobileoa/writing.png");
		}
		else if(v==btCleanWriting)
		{
			myView.clear();
		}
	}

	class PaintView extends View {
		private Paint paint;
		private Canvas cacheCanvas;
		private Bitmap cachebBitmap;
		private Path path;

		public Bitmap getCachebBitmap() {
			return cachebBitmap;
		}

		public PaintView(Context context) {
			super(context);					
			init();			
		}

		private void init(){
			setDrawingCacheEnabled(true);
			buildDrawingCache();     
			paint = new Paint();
			paint.setAntiAlias(true);
			paint.setStrokeWidth(3);
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLACK);					
			path = new Path();
			cachebBitmap = Bitmap.createBitmap(480, 320, Config.ARGB_8888);	
			cacheCanvas = new Canvas(cachebBitmap);
			cacheCanvas.drawColor(Color.TRANSPARENT);
		}
		public void clear() {
			if (cacheCanvas != null) {
				cachebBitmap = Bitmap.createBitmap(480, 320, Config.ARGB_8888);	
				cacheCanvas = new Canvas(cachebBitmap);
				cacheCanvas.drawColor(Color.TRANSPARENT);
				invalidate();			
			}
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawBitmap(cachebBitmap, 0, 0, null);
			canvas.drawPath(path, paint);
		}

		@Override
		protected void onSizeChanged(int w, int h, int oldw, int oldh) {

			int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
			int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
			if (curW >= w && curH >= h) {
				return;
			}

			if (curW < w)
				curW = w;
			if (curH < h)
				curH = h;

			Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
			Canvas newCanvas = new Canvas();
			newCanvas.setBitmap(newBitmap);
			if (cachebBitmap != null) {
				newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
			}
			cachebBitmap = newBitmap;
			cacheCanvas = newCanvas;
		}

		private float cur_x, cur_y;

		@Override
		public boolean onTouchEvent(MotionEvent event) {

			float x = event.getX();
			float y = event.getY();

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				tvWritingTip.setVisibility(View.INVISIBLE);
				cur_x = x;
				cur_y = y;
				path.moveTo(cur_x, cur_y);
				break;
			}

			case MotionEvent.ACTION_MOVE: {
				path.quadTo(cur_x, cur_y, x, y);
				cur_x = x;
				cur_y = y;
				break;
			}

			case MotionEvent.ACTION_UP: {
				cacheCanvas.drawPath(path, paint);
				path.reset();
				break;
			}
			}

			invalidate();
			return true;
		}
	}

	/* 自定义继承View 的MyView*/    
	/*private class MyView extends View implements View.OnTouchListener{     
		public MyView(Context context){     
			super(context) ;     
		}     

		重写onDraw（）    
		@Override    
		protected void onDraw(Canvas canvas)     
		{        
			super.onDraw(canvas);     
			设置背景为白色    
			canvas.drawColor(Color.WHITE);     

			Paint paint=new Paint();     
			去锯齿    
			paint.setAntiAlias(true);     
			设置paint的颜色    
			paint.setColor(Color.RED);     
			设置paint的 style 为STROKE：空心    
			paint.setStyle(Paint.Style.STROKE);     
			设置paint的外框宽度    
			paint.setStrokeWidth(3);     

			画一个空心圆形    
			canvas.drawCircle(40, 40, 30, paint);     
			画一个空心正方形    
			canvas.drawRect(10, 90, 70, 150, paint);     
			画一个空心长方形    
			canvas.drawRect(10, 170, 70,200, paint);     
			画一个空心椭圆形    
			canvas.drawOval(new RectF(10,220,70,250), paint);     
			画一个空心三角形    
			Path path=new Path();     
			path.moveTo(10, 330);     
			path.lineTo(70,330);     
			path.lineTo(40,270);     
			path.close();     
			canvas.drawPath(path, paint);     
			画一个空心梯形    
			Path path1=new Path();     
			path1.moveTo(10, 410);     
			path1.lineTo(70,410);     
			path1.lineTo(55,350);     
			path1.lineTo(25, 350);     
			path1.close();     
			canvas.drawPath(path1, paint);     

			设置paint的颜色    
			paint.setColor(Color.BLUE);     
			设置paint 的style为 FILL：实心    
			paint.setStyle(Paint.Style.FILL);     
			画一个实心圆    
			canvas.drawCircle(120,40,30, paint);     
			画一个实心正方形    
			canvas.drawRect(90, 90, 150, 150, paint);     
			画一个实心长方形    
			canvas.drawRect(90, 170, 150,200, paint);     
			画一个实心椭圆    
			RectF re2=new RectF(90,220,150,250);     
			canvas.drawOval(re2, paint);     
			画一个实心三角形    
			Path path2=new Path();     
			path2.moveTo(90, 330);     
			path2.lineTo(150,330);     
			path2.lineTo(120,270);     
			path2.close();     
			canvas.drawPath(path2, paint);     
			画一个实心梯形    
			Path path3=new Path();     
			path3.moveTo(90, 410);     
			path3.lineTo(150,410);     
			path3.lineTo(135,350);     
			path3.lineTo(105, 350);     
			path3.close();     
			canvas.drawPath(path3, paint);     

			设置渐变色    
			Shader mShader=new LinearGradient(0,0,100,100,     
					new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},     
					null,Shader.TileMode.REPEAT);     
			//Shader.TileMode三种模式      
			//REPEAT:沿着渐变方向循环重复      
			//CLAMP:如果在预先定义的范围外画的话，就重复边界的颜色      
			//MIRROR:与REPEAT一样都是循环重复，但这个会对称重复      
			paint.setShader(mShader);//用Shader中定义定义的颜色来话      

			画一个渐变色圆    
			canvas.drawCircle(200,40,30, paint);     
			画一个渐变色正方形    
			canvas.drawRect(170, 90, 230, 150, paint);     
			画一个渐变色长方形    
			canvas.drawRect(170, 170, 230,200, paint);     
			画一个渐变色椭圆    
			RectF re3=new RectF(170,220,230,250);     
			canvas.drawOval(re3, paint);     
			画一个渐变色三角形    
			Path path4=new Path();     
			path4.moveTo(170,330);     
			path4.lineTo(230,330);     
			path4.lineTo(200,270);     
			path4.close();     
			canvas.drawPath(path4, paint);     
			画一个渐变色梯形    
			Path path5=new Path();     
			path5.moveTo(170, 410);     
			path5.lineTo(230,410);     
			path5.lineTo(215,350);     
			path5.lineTo(185, 350);     
			path5.close();     
			canvas.drawPath(path5, paint);     

			写字    
			paint.setTextSize(24);     
			canvas.drawText("圆形", 240, 50, paint);     
			canvas.drawText("正方形", 240, 120, paint);     
			canvas.drawText("长方形", 240, 190, paint);     
			canvas.drawText("椭圆形", 240, 250, paint);     
			canvas.drawText("三角形", 240, 320, paint);     
			canvas.drawText("梯形", 240, 390, paint);       
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			return false;
		}     
	}     */


	@Override
	public void refresh(Object... param) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLayoutInt() {
		// TODO Auto-generated method stub
		return R.layout.handwrittensigndetail;
	}

}
